<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>任务信息</span>
        <el-button v-if="detailFlag" style="float: right; padding: 3px 0" type="text" @click="edit">编辑</el-button>
        <el-button v-if="editFlag" style="float: right; padding: 3px 0" type="text" @click="backDetail">详情</el-button>
      </div>
      <div class="Task_detail" v-if="detailFlag">
        <el-col :span="22" :offset="1" :xs="24">
          <el-descriptions title="" :column="3" :labelStyle="{ fontWeight: 'bold' }">
            <el-descriptions-item label="任务名称">{{ form.workEffortName }}</el-descriptions-item>
            <!-- <el-descriptions-item label="项目阶段">{{ phaseIdFormat(form) }}</el-descriptions-item> -->
            <el-descriptions-item label="任务序号">{{ form.sequenceNum }}</el-descriptions-item>
            <el-descriptions-item label="优先级">{{ form.priority }}</el-descriptions-item>
            <el-descriptions-item label="范围">{{ form.scopeEnumId }}</el-descriptions-item>
            <el-descriptions-item label="状态">{{ currentStatusIdFormat(form) }}</el-descriptions-item>
            <el-descriptions-item label="预计开始时间"><span>{{ form.estimatedStartDate }}</span></el-descriptions-item>
            <el-descriptions-item label="预计完成时间" span="2"><span>{{
              form.estimatedCompletionDate }}</span></el-descriptions-item>
            <el-descriptions-item label="描述" span="2"><span>{{ form.description }}</span></el-descriptions-item>
          </el-descriptions>

        </el-col>
      </div>
      <el-col :span="22" :offset="1" :xs="24">
        <div class="Task_edit" v-if="editFlag">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="任务名称" prop="workEffortName">
                  <el-input v-model="form.workEffortName" placeholder="请输入任务名称" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <!-- <el-form-item label="项目阶段" prop="workEffortParentId">
                  <el-select v-model="form.workEffortParentId" placeholder="请选择项目阶段">
                    <el-option v-for="dict in phaseOptions" :key="dict.phaseId" :label="dict.phaseName"
                      :value="dict.phaseId"></el-option>
                  </el-select>
                </el-form-item> -->
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="任务序号" prop="sequenceNum">
                  <el-input v-model="form.sequenceNum" placeholder="请输入任务序号" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="优先级" prop="priority">
                  <el-input v-model="form.priority" placeholder="请输入优先级" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="范围" prop="scopeEnumId">
                  <el-input v-model="form.scopeEnumId" placeholder="请输入范围" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="预计开始时间" prop="estimatedStartDate">
                  <el-date-picker clearable v-model="form.estimatedStartDate" type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss" default-time="00:00:00" placeholder="请选择预计开始时间">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="预计完成时间" prop="estimatedCompletionDate">
                  <el-date-picker clearable v-model="form.estimatedCompletionDate" type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss" default-time="00:00:00" placeholder="请选择预计完成时间">
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="20">
                <el-form-item label="描述" prop="description">
                  <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="状态" prop="currentStatusId">
                  <el-select v-model="form.currentStatusId" placeholder="请选择状态">
                    <el-option v-for="dict in currentStatusIdOptions" :key="dict.enumCode" :label="dict.description"
                      :value="dict.enumCode"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="24">
                <el-form-item size="large" align="center">
                  <el-button type="primary" @click="saveForm">保 存</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </el-col>
    </el-card>
  </div>
</template>

<script>
import { getTask, addTask, updateTask } from "@/api/ehr/task";
import { getCust_request_work_effort, addCust_request_work_effort } from "@/api/ehr/cust_request_work_effort";

export default {
  name: "TaskCard",
  props: {
    // taskId: {
    //   type: String
    // },
    projectId: {
      type: String
    },
  },
  data() {
    return {
      taskId: null,
      // 遮罩层
      loading: true,
      // 人员字典
      personOptions: [],
      // 计划类型业务字典
      workEffortTypeIdOptions: [],
      // 状态业务字典
      currentStatusIdOptions: [],
      // 项目阶段字典
      phaseOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分是详情
      detailFlag: true,
      // 操作标识,用于区分编辑状态
      editFlag: false,

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workEffortTypeId: [
          { required: true, message: "计划类型不能为空", trigger: "change" }
        ],

        workEffortName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ],

      }
    };
  },
  created() {
    this.reset();

    // 计划类型业务字典
    this.getEnumDicts("WORK_EFFORT_TYPE").then((response) => {
      this.workEffortTypeIdOptions = response.data;
    });
    // 状态业务字典
    this.getEnumDicts("WORK_EFFORT_STATUS").then((response) => {
      this.currentStatusIdOptions = response.data;
    });

    // PhaseAndProject
    // console.info("project id " + this.form.projectId)

    // 从请求URL 参数中获取 custRequestId
    this.custRequestId = this.$route.query.custRequestId;
    console.info("custRequestId:" + this.custRequestId)
    // 若已获取 客户请求ID，则根据 客户请求ID 查询客户请求任务是否已创建
    if (this.custRequestId) {
      this.getCustRequestTask(this.custRequestId);
    }

  },
  methods: {

    getCustRequestTask(custRequestId) {
      getCust_request_work_effort(custRequestId).then(response => {
        this.task_form = response.data;
        if (this.task_form) {
          this.detailFlag = true;
          this.editFlag = false;
          this.taskId = this.task_form.workEffortId;
        }
        console.info("getCustRequestTask back taskId :" + this.taskId)
        if (this.taskId) {
          this.getTask(this.taskId);
        } else {
          this.addCustRequestTask(custRequestId);
          console.info("addCustRequestTask back get taskId :" + this.taskId)
        }

      });
    },

    addCustRequestTask(custRequestId) {
      var form = {};
      form.custRequestId = custRequestId
      // console.info("addCustRequestTask custRequestId :" + form.custRequestId)
      addCust_request_work_effort(form).then(response => {
        form = response.data;
        if (form) {
          this.detailFlag = true;
          this.editFlag = false;
          this.taskId = form.workEffortId;
          console.info("request panel addCust_request_work_effort taskId :" + this.taskId)
        }
      });
    },

    getTask(taskId) {
      this.reset();
      console.info("card task taskId:" + taskId);
      getTask(taskId).then(response => {
        if (response.data) {
          this.form = response.data;
          this.title = "任务详情";
        }
        // console.info("project id " + this.form.projectId);
        // 项目阶段业务字典
        // this.getEntityDictsForSearch("PhaseAndProject:workEffortParentId:" + this.form.projectId).then((response) => {
        //   this.phaseOptions = response.data;
        // });
      });
    },

    workEffortTypeIdFormat(row, column) {
      let val = null;
      this.workEffortTypeIdOptions.forEach((item, index) => {
        if (row.workEffortTypeId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    currentStatusIdFormat(row, column) {
      let val = null;
      this.currentStatusIdOptions.forEach((item, index) => {
        if (row.currentStatusId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    phaseIdFormat(row, column) {
      let val = null;
      this.phaseOptions.forEach((item, index) => {
        if (row.workEffortParentId === item.phaseId) {
          val = item.phaseName;
        }
      });
      return val;
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        phaseName: null,
        projectName: null,
        projectId: null,
        workEffortId: null,
        workEffortTypeId: null,
        currentStatusId: null,
        lastStatusUpdate: null,
        workEffortPurposeTypeId: null,
        workEffortParentId: null,
        scopeEnumId: null,
        priority: null,
        percentComplete: null,
        workEffortName: null,
        showAsEnumId: null,
        sendNotificationEmail: null,
        description: null,
        locationDesc: null,
        estimatedStartDate: null,
        estimatedCompletionDate: null,
        actualStartDate: null,
        actualCompletionDate: null,
        estimatedMilliSeconds: null,
        estimatedSetupMillis: null,
        estimateCalcMethod: null,
        actualMilliSeconds: null,
        actualSetupMillis: null,
        totalMilliSecondsAllowed: null,
        totalMoneyAllowed: null,
        moneyUomId: null,
        specialTerms: null,
        timeTransparency: null,
        universalId: null,
        sourceReferenceId: null,
        fixedAssetId: null,
        facilityId: null,
        infoUrl: null,
        recurrenceInfoId: null,
        tempExprId: null,
        runtimeDataId: null,
        noteId: null,
        serviceLoaderName: null,
        quantityToProduce: null,
        quantityProduced: null,
        quantityRejected: null,
        reservPersons: null,
        reserv2ndPPPerc: null,
        reservNthPPPerc: null,
        accommodationMapId: null,
        accommodationSpotId: null,
        revisionNumber: null,
        createdDate: null,
        createdByUserLogin: null,
        lastModifiedDate: null,
        lastModifiedByUserLogin: null,
        sequenceNum: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null
      };
      //this.resetForm("form");
    },

    /** 保存按钮 */
    saveForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          this.form.workEffortTypeId = "task";

          if (this.form.workEffortId != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
            });
          }

          this.loading = false;
          this.editFlag = false;
          this.detailFlag = true;
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;

          addTask(this.form).then((response) => {
            this.$modal.msgSuccess("申请提交成功！");
            this.open = false;
          });
          this.loading = false;

          this.close();
        }
      });
    },

    /** 编辑 */
    edit() {
      this.editFlag = true;
      this.detailFlag = false;
    },
    backDetail() {
      this.editFlag = false;
      this.detailFlag = true;
    },
    resetForm() {
      this.$refs['form'].resetFields()
    },
    close() {
      this.$tab.closePage();
    },

  }
};
</script>
<style scoped lang="scss">
.spacing {
  display: inline-block;
  width: 40px;
}
</style>
